<template>
  <div class="tab-bar-item">
    <div v-if="!isActive" @click="itemClick"><slot name="item-icon"></slot></div>
    <div v-else><slot name="item-icon-active"></slot></div>
    <!-- 将插槽用div包裹防止呗替换，把属性写在div中 -->
    <!-- <div :class="{active: isActive}"><slot name="item-text"></slot></div> -->
    <div :style="activeStyle"><slot name="item-text"></slot></div>
  </div>
</template>

<script>
export default {
  name: 'TabBarItem',
  props:{
    path: String,
    activeColor: {
      type: String,
      default: 'red'
    }
  },
  data(){
    return{
      // isActive: true,
    }
  },
  methods: {
    itemClick() {
      this.$router.replace(this.path).catch(err => err)
      console.log(this.path);
    }
  },
  computed: {
    isActive(){
      //$this.$route拿到活跃的那个路由
      return this.$route.path.indexOf(this.path) !== -1
    },
    activeStyle(){
      //如果处于激活状态，设置字体颜色
      return this.isActive ? {color: this.activeColor} : {}
    }
  }
}
</script>

<style scoped>
  .tab-bar-item {
    flex: 1;
    text-align: center;
    height: 49px;
    /* line-height: 49px; */
    font-size: 14px;
  }
  .tab-bar-item img {
    height: 24px;
    width: 24px;
    margin-top: 3px;
    vertical-align: middle;
  }
  /* .active {
    color: red;
  } */
</style>